package com.cloud.core.aop;

import com.alibaba.fastjson.JSONObject;
import com.cloud.business.common.CommonConst;
import com.cloud.core.utils.CustomHttpUtils;
import com.cloud.core.utils.CustomStringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 应用模块名称
 * Copyright: Copyright (C) 2020 DLANGEL, Inc. All rights reserved.
 * Company: 大连安琪科技有限公司
 *
 * @author Rex
 * @since 2020/4/21 17:30 
 */
@Aspect
@Component
public aspect LogAspect {

    /**
     * 前置通知：目标方法执行之前执行以下方法体的内容
     *
     * @param jp
     * @return
     * @author Rex
     */
    @Before("execution(* com.cloud.business.controller.*.*.*(..))")
    public void beforeMethod(JoinPoint jp) {
        log(jp, "开始", "dlog", null);
    }

    private void log(JoinPoint jp, String result, String log, Object objResult) {
        String methodName = jp.getSignature().getName();
        String classStr = jp.getSignature().getDeclaringTypeName();
        int index = classStr.lastIndexOf(".");
        String moduleName = classStr.substring(index + 1);
        Object[] args = jp.getArgs();
        // 参数名
        StringBuilder stringBuilder = new StringBuilder();
        for (Object obj : args) {
            stringBuilder.append(CustomStringUtils.getArgs(obj));
        }
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("operationId", request.getHeader("operationId"));
        jsonObject.put("userName", "aop");
        jsonObject.put("projectName", "乌鲁木齐话务");
        jsonObject.put("moduleName", moduleName);
        jsonObject.put("method", methodName);
        String content = stringBuilder.toString();
        if (null != objResult) {
            content += ",result:" + JSONObject.toJSONString(objResult);
        }
        jsonObject.put("content", content);
        jsonObject.put("result", result);
        CustomHttpUtils.sendHttpPostJson(CommonConst.Log.url + log, jsonObject.toString());
    }

    /**
     * 返回通知：目标方法正常执行完毕时执行以下代码
     *
     * @param jp
     * @param result
     * @return
     * @author Rex
     */
    @AfterReturning(value = "execution(* com.cloud.business.controller.*.*.*(..))", returning = "result")
    public void afterReturningMethod(JoinPoint jp, Object result) {
        log(jp, "结束", "dlog", result);
    }

    /**
     * 异常通知：目标方法发生异常的时候执行以下代码  
     *
     * @param jp
     * @param e
     * @return
     * @author Rex
     */
    @AfterThrowing(value = "execution(* com.cloud.business.controller.*.*.*(..))", throwing = "e")
    public void afterThrowingMethod(JoinPoint jp, Exception e) {
        log(jp, "结束", "elog", e.getMessage());
    }
}
